package com.mswyt.utils.msCollection;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName: MsMap
 * @Description: 可以存放多个key的map
 */
public class MsMap<K, KK, V> {

    private Map<K, Map<KK, V>> fMap;

    //是否创建线程安全的集合
    private Boolean threadSafe;

    public MsMap() {
        this.fMap = new HashMap<>();
        this.threadSafe = false;
    }

    public MsMap(Boolean threadSafe) {
        this.fMap = new ConcurrentHashMap<>();
        this.threadSafe = true;
    }

    /***
     *构建{k1,K2,value}格式的map
     * @param k
     * @param kk
     * @param v
     * @return
     * @author mashuai
     */

    public void put(K k, KK kk, V v) {
        Map<KK, V> vMap = threadSafe ? new ConcurrentHashMap<>() : new HashMap<>();
        vMap.put(kk, v);
        fMap.put(k, vMap);
    }

    /**
     * 获取k对应的map
     **/
    public Map<KK, V> get(K k) {
        return fMap.get(k);
    }


    /**
     * 获取k对应的map
     **/
    public V get(K k, KK kk) {
        Map<KK, V> kkvMap = get(k);
        if (kkvMap == null) return null;
        return kkvMap.get(kk);
    }

    /**
     * 是否包含某个key
     **/
    public Boolean containsKey(K k) {
        return fMap.containsKey(k);
    }
    /**
     * 是否包含某个key
     **/
    public Boolean containsKey(K k, KK kk) {
        if (fMap.containsKey(k)) {
            Map<KK, V> kkvMap = fMap.get(k);
            return kkvMap.containsKey(kk);
        }
        return false;
    }
}
